﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;

namespace winfromText1
{
    class DBHelper
    {
        // 数据库连接
       private static readonly string conString = "server=172.31.0.240;database=ERP_P1;uid=hyj;pwd=hyj123.";

        /// <summary>
        /// 填充数据库数据到表格
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static DataTable GetData(string sql)
        {
            SqlDataAdapter adapter = new SqlDataAdapter(sql, conString);
            DataTable dt = new DataTable();
            adapter.Fill(dt);
            return dt;

        }

        /// <summary>
        /// 对数据库数据进行增加删除修改的操作
        /// </summary>
        /// <param name="sql"></param>
        /// <returns></returns>
        public static int AddOrDelectOrUpdate(string sql)
        {
            // 连接数据库
            SqlConnection connection = new SqlConnection(conString);

            // 数据库语句对连接的哪个数据库起作用
            SqlCommand command = new SqlCommand(sql,connection);
            //打开数据库连接
            connection.Open();

            // 返回数据库受影响的行数
            var res = command.ExecuteNonQuery();

            // 关闭数据库连接
            connection.Close();

            return res;


        }


    }
}
